import 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import router from './router'
import fastclick from 'fastclick'
import Vant from 'vant'
import 'vant/lib/index.css'
import 'vant/lib/icon/local.css'
import VueLazyload from 'vue-lazyload'
import objectUtil from '_u/common'
import axios from 'axios'
import {post, put, get, deletes} from '_u/http'
import Qs from 'qs'

Vue.use(Vant)
fastclick.attach(document.body)
Vue.use(VueLazyload, {
  loading: require('./assets/logo.png'),
  error: require('./assets/logo.png')
})
Vue.prototype.$objectUtil = objectUtil
Vue.prototype.$qs = Qs
Vue.prototype.$post = post
Vue.prototype.$put = put
Vue.prototype.$get = get
Vue.prototype.$deletes = deletes

// document.addEventListener('DOMContentLoaded', function () {
//   var html = document.documentElement
//   var windowWidth = html.clientWidth
//   html.style.fontSize = windowWidth / 6.4 + 'px'
// }, false)

document.addEventListener('touchmove', function(event) {
  // 判断默认行为是否可以被禁用
  if (event.cancelable) {
    // 判断默认行为是否已经被禁用
    if (!event.defaultPrevented) {
      event.preventDefault()
    }
  }
}, false)

axios.interceptors.request.use(
  config => {
    let token = localStorage.getItem('x-token')
    if (token) {
      config.headers['x-token'] = token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          router.replace({
            path: '/login',
            query: {redirect: router.currentRoute.fullPath}
          })
          break
        case 500:
          if (error.response.data.message === '该请求需要先登录才可以访问,用户未登录或token不存在！') {
            if (router.currentRoute.name) {
              router.replace({
                name: 'login',
                query: {redirect: router.currentRoute.fullPath}
              })
            } else {
              router.push('/login')
            }
          }
          break
      }
    }
    return Promise.reject(error.response.data)
  })

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
